package org.lai.Exception;

import lombok.extern.slf4j.Slf4j;
import org.lai.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice // 表明该类是一个全局异常处理器
public class GlobalExceptionHander {
    @ExceptionHandler
    public Result handerException(Exception e) {
        log.error("程序出错了:{}",e);
        return Result.error("服务器出错啦,请联系管理员");
    }
    @ExceptionHandler
    public Result headleDuplicateKeyException(DuplicateKeyException e) {
        log.error("程序出错了:{}",e);
        String msg = e.getMessage();
        int i = msg.indexOf("Duplicate entry");
        String err = msg.substring(i);
        String arr = err.split(" ")[2];
        return Result.error(arr + "已存在");
    }
    @ExceptionHandler
    public Result handleException(haveStudentException e) {
        log.error("对不起, 该班级下有学生, 不能直接删除:", e);
        return Result.error(e.getMessage());
    }
}
